home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxclimag.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  42.4 KB  |  1,361 lines

  1. /* Copyright (C) 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxclimag.c,v 1.2 2000/09/19 19:00:34 lpd Exp $ */
  20. /* Higher-level image operations for band lists */
  21. #include "math_.h"
  22. #include "memory_.h"
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gscspace.h"
  26. #include "gscdefs.h"        /* for image type table */
  27. #include "gxarith.h"
  28. #include "gxcspace.h"
  29. #include "gxdevice.h"
  30. #include "gxdevmem.h"        /* must precede gxcldev.h */
  31. #include "gxcldev.h"
  32. #include "gxclpath.h"
  33. #include "gxfmap.h"
  34. #include "gxiparam.h"
  35. #include "gxpath.h"
  36. #include "stream.h"
  37. #include "strimpl.h"        /* for sisparam.h */
  38. #include "sisparam.h"
  39.  
  40. extern_gx_image_type_table();
  41.  
  42. /* Define whether we should use high-level images. */
  43. /* (See below for additional restrictions.) */
  44. static bool USE_HL_IMAGES = true;
  45.  
  46. /* Forward references */
  47. private int cmd_put_set_data_x(P3(gx_device_clist_writer * cldev,
  48.                   gx_clist_state * pcls, int data_x));
  49. private int cmd_put_color_mapping(P3(gx_device_clist_writer * cldev,
  50.                      const gs_imager_state * pis,
  51.                      bool write_rgb_to_cmyk));
  52. private bool check_rect_for_trivial_clip(P5(
  53.     const gx_clip_path *pcpath,  /* May be NULL, clip to evaluate */
  54.     int px, int py, int qx, int qy  /* corners of box to test */
  55. ));
  56.  
  57. /* ------ Driver procedures ------ */
  58.  
  59. int
  60. clist_fill_mask(gx_device * dev,
  61.         const byte * data, int data_x, int raster, gx_bitmap_id id,
  62.         int x, int y, int width, int height,
  63.         const gx_drawing_color * pdcolor, int depth,
  64.         gs_logical_operation_t lop, const gx_clip_path * pcpath)
  65. {
  66.     gx_device_clist_writer * const cdev =
  67.     &((gx_device_clist *)dev)->writer;
  68.     const byte *orig_data = data;    /* for writing tile */
  69.     int orig_data_x = data_x;    /* ditto */
  70.     int orig_x = x;        /* ditto */
  71.     int orig_width = width;    /* ditto */
  72.     int orig_height = height;    /* ditto */
  73.     int log2_depth = ilog2(depth);
  74.     int y0;
  75.     int data_x_bit;
  76.     byte copy_op =
  77.     (depth > 1 ? cmd_op_copy_color_alpha :
  78.      gx_dc_is_pure(pdcolor) ? cmd_op_copy_mono :
  79.      cmd_op_copy_mono + cmd_copy_ht_color);
  80.     bool slow_rop =
  81.     cmd_slow_rop(dev, lop_know_S_0(lop), pdcolor) ||
  82.     cmd_slow_rop(dev, lop_know_S_1(lop), pdcolor);
  83.     fit_copy(dev, data, data_x, raster, id, x, y, width, height);
  84.     y0 = y;            /* must do after fit_copy */
  85.  
  86.     /* If non-trivial clipping & complex clipping disabled, default */
  87.     if (((cdev->disable_mask & clist_disable_complex_clip) &&
  88.      !check_rect_for_trivial_clip(pcpath, x, y, x + width, y + height)) ||
  89.     gs_debug_c('`')
  90.     )
  91.     return gx_default_fill_mask(dev, data, data_x, raster, id,
  92.                     x, y, width, height, pdcolor, depth,
  93.                     lop, pcpath);
  94.     if (cmd_check_clip_path(cdev, pcpath))
  95.     cmd_clear_known(cdev, clip_path_known);
  96.     data_x_bit = data_x << log2_depth;
  97.     FOR_RECTS {
  98.     int dx = (data_x_bit & 7) >> log2_depth;
  99.     const byte *row = data + (y - y0) * raster + (data_x_bit >> 3);
  100.     int code;
  101.  
  102.     TRY_RECT {
  103.         code = cmd_update_lop(cdev, pcls, lop);
  104.     } HANDLE_RECT(code);
  105.     if (depth > 1 && !pcls->color_is_alpha) {
  106.         byte *dp;
  107.  
  108.         TRY_RECT {
  109.         code =
  110.             set_cmd_put_op(dp, cdev, pcls, cmd_opv_set_copy_alpha, 1);
  111.         } HANDLE_RECT(code);
  112.         pcls->color_is_alpha = 1;
  113.     }
  114.     TRY_RECT {
  115.         code = cmd_do_write_unknown(cdev, pcls, clip_path_known);
  116.         if (code >= 0)
  117.         code = cmd_do_enable_clip(cdev, pcls, pcpath != NULL);
  118.     } HANDLE_RECT(code);
  119.     TRY_RECT {
  120.         code = cmd_put_drawing_color(cdev, pcls, pdcolor);
  121.     } HANDLE_RECT(code);
  122.     pcls->colors_used.slow_rop |= slow_rop;
  123.     /*
  124.      * Unfortunately, painting a character with a halftone requires the
  125.      * use of two bitmaps, a situation that we can neither represent in
  126.      * the band list nor guarantee will both be present in the tile
  127.      * cache; in this case, we always write the bits of the character.
  128.      *
  129.      * We could handle more RasterOp cases here directly, but it
  130.      * doesn't seem worth the trouble right now.
  131.      */
  132.     if (id != gx_no_bitmap_id && gx_dc_is_pure(pdcolor) &&
  133.         lop == lop_default
  134.         ) {            /* This is a character.  ****** WRONG IF HALFTONE CELL. ***** */
  135.         /* Put it in the cache if possible. */
  136.         ulong offset_temp;
  137.  
  138.         if (!cls_has_tile_id(cdev, pcls, id, offset_temp)) {
  139.         gx_strip_bitmap tile;
  140.  
  141.         tile.data = (byte *) orig_data;    /* actually const */
  142.         tile.raster = raster;
  143.         tile.size.x = tile.rep_width = orig_width;
  144.         tile.size.y = tile.rep_height = orig_height;
  145.         tile.rep_shift = tile.shift = 0;
  146.         tile.id = id;
  147.         TRY_RECT {
  148.             code = clist_change_bits(cdev, pcls, &tile, depth);
  149.         } HANDLE_RECT_UNLESS(code,
  150.             (code != gs_error_VMerror || !cdev->error_is_retryable) );
  151.         if (code < 0) {
  152.             /* Something went wrong; just copy the bits. */
  153.             goto copy;
  154.         }
  155.         } {
  156.         gx_cmd_rect rect;
  157.         int rsize;
  158.         byte op = copy_op + cmd_copy_use_tile;
  159.  
  160.         /* Output a command to copy the entire character. */
  161.         /* It will be truncated properly per band. */
  162.         rect.x = orig_x, rect.y = y0;
  163.         rect.width = orig_width, rect.height = yend - y0;
  164.         rsize = 1 + cmd_sizexy(rect);
  165.         TRY_RECT {
  166.             code = (orig_data_x ?
  167.                 cmd_put_set_data_x(cdev, pcls, orig_data_x) : 0);
  168.             if (code >= 0) {
  169.             byte *dp;
  170.  
  171.             code = set_cmd_put_op(dp, cdev, pcls, op, rsize);
  172.             /*
  173.              * The following conditional is unnecessary: the two
  174.              * statements inside it should go outside the
  175.              * HANDLE_RECT.  They are here solely to pacify
  176.              * stupid compilers that don't understand that dp
  177.              * will always be set if control gets past the
  178.              * HANDLE_RECT.
  179.              */
  180.             if (code >= 0) {
  181.                 dp++;
  182.                 cmd_putxy(rect, dp);
  183.             }
  184.             }
  185.         } HANDLE_RECT(code);
  186.         pcls->rect = rect;
  187.         goto end;
  188.         }
  189.     }
  190. copy:    /*
  191.      * The default fill_mask implementation uses strip_copy_rop;
  192.      * this is exactly what we want.
  193.      */
  194.     TRY_RECT {
  195.         NEST_RECT {
  196.         code = gx_default_fill_mask(dev, row, dx, raster,
  197.                     (y == y0 && height == orig_height &&
  198.                      dx == orig_data_x ? id :
  199.                      gx_no_bitmap_id),
  200.                         x, y, width, height, pdcolor,
  201.                         depth, lop, pcpath);
  202.         } UNNEST_RECT;
  203.     } HANDLE_RECT(code);
  204. end:
  205.     ;
  206.     } END_RECTS;
  207.     return 0;
  208. }
  209.  
  210. /* ------ Bitmap image driver procedures ------ */
  211.  
  212. /* Define the structure for keeping track of progress through an image. */
  213. typedef struct clist_image_enum_s {
  214.     gx_image_enum_common;
  215.     /* Arguments of begin_image */
  216.     gs_memory_t *memory;
  217.     gs_pixel_image_t image;    /* only uses Width, Height, Interpolate */
  218.     gx_drawing_color dcolor;    /* only pure right now */
  219.     gs_int_rect rect;
  220.     const gs_imager_state *pis;
  221.     const gx_clip_path *pcpath;
  222.     /* Set at creation time */
  223.     gs_image_format_t format;
  224.     gs_int_point support;    /* extra source pixels for interpolation */
  225.     int bits_per_plane;        /* bits per pixel per plane */
  226.     gs_matrix matrix;        /* image space -> device space */
  227.     bool uses_color;
  228.     clist_color_space_t color_space;
  229.     int ymin, ymax;
  230.     bool map_rgb_to_cmyk;
  231.     gx_colors_used_t colors_used;
  232.     /* begin_image command prepared & ready to output */
  233.     /****** SIZE COMPUTATION IS WRONG, TIED TO gximage.c, gsmatrix.c ******/
  234.     byte begin_image_command[3 +
  235.                 /* Width, Height */
  236.                 2 * cmd_sizew_max +
  237.                 /* ImageMatrix */
  238.                 1 + 6 * sizeof(float) +
  239.                 /* Decode */
  240.                 (GS_IMAGE_MAX_COMPONENTS + 3) / 4 +
  241.                   GS_IMAGE_MAX_COMPONENTS * 2 * sizeof(float) +
  242.                 /* MaskColors */
  243.                 GS_IMAGE_MAX_COMPONENTS * cmd_sizew_max +
  244.                 /* rect */
  245.                 4 * cmd_sizew_max];
  246.     int begin_image_command_length;
  247.     /* Updated dynamically */
  248.     int y;
  249.     bool color_map_is_known;
  250. } clist_image_enum;
  251. gs_private_st_suffix_add3(st_clist_image_enum, clist_image_enum,
  252.               "clist_image_enum", clist_image_enum_enum_ptrs,
  253.               clist_image_enum_reloc_ptrs, st_gx_image_enum_common,
  254.               pis, pcpath, color_space.space);
  255.  
  256. private image_enum_proc_plane_data(clist_image_plane_data);
  257. private image_enum_proc_end_image(clist_image_end_image);
  258. private const gx_image_enum_procs_t clist_image_enum_procs =
  259. {
  260.     clist_image_plane_data, clist_image_end_image
  261. };
  262.  
  263. /* Forward declarations */
  264. private bool image_band_box(P5(gx_device * dev, const clist_image_enum * pie,
  265.                    int y, int h, gs_int_rect * pbox));
  266. private int begin_image_command(P3(byte *buf, uint buf_size,
  267.                    const gs_image_common_t *pic));
  268. private int cmd_image_plane_data(P8(gx_device_clist_writer * cldev,
  269.                     gx_clist_state * pcls,
  270.                     const gx_image_plane_t * planes,
  271.                     const gx_image_enum_common_t * pie,
  272.                     uint bytes_per_plane,
  273.                     const uint * offsets, int dx, int h));
  274. private uint clist_image_unknowns(P2(gx_device *dev,
  275.                      const clist_image_enum *pie));
  276. private int write_image_end_all(P2(gx_device *dev,
  277.                    const clist_image_enum *pie));
  278.  
  279. /*
  280.  * Since currently we are limited to writing a single subrectangle of the
  281.  * image for each band, images that are rotated by angles other than
  282.  * multiples of 90 degrees may wind up writing many copies of the data.
  283.  * Eventually we will fix this by breaking up the image into multiple
  284.  * subrectangles, but for now, don't use the high-level approach if it would
  285.  * cause the data to explode because of this.
  286.  */
  287. private bool
  288. image_matrix_ok_to_band(const gs_matrix * pmat)
  289. {
  290.     double t;
  291.  
  292.     /* Don't band if the matrix is (nearly) singular. */
  293.     if (fabs(pmat->xx * pmat->yy - pmat->xy * pmat->yx) < 0.001)
  294.     return false;
  295.     if (is_xxyy(pmat) || is_xyyx(pmat))
  296.     return true;
  297.     t = (fabs(pmat->xx) + fabs(pmat->yy)) /
  298.     (fabs(pmat->xy) + fabs(pmat->yx));
  299.     return (t < 0.2 || t > 5);
  300. }
  301.  
  302. /* Start processing an image. */
  303. int
  304. clist_begin_typed_image(gx_device * dev,
  305.             const gs_imager_state * pis, const gs_matrix * pmat,
  306.            const gs_image_common_t * pic, const gs_int_rect * prect,
  307.           const gx_drawing_color * pdcolor, const gx_clip_path * pcpath,
  308.             gs_memory_t * mem, gx_image_enum_common_t ** pinfo)
  309. {
  310.     const gs_pixel_image_t * const pim = (const gs_pixel_image_t *)pic;
  311.     gx_device_clist_writer * const cdev =
  312.     &((gx_device_clist *)dev)->writer;
  313.     clist_image_enum *pie = 0;
  314.     int base_index;
  315.     bool indexed;
  316.     bool masked = false;
  317.     bool has_alpha = false;
  318.     int num_components;
  319.     int bits_per_pixel;
  320.     bool uses_color;
  321.     bool varying_depths = false;
  322.     gs_matrix mat;
  323.     gs_rect sbox, dbox;
  324.     gs_image_format_t format;
  325.     gx_color_index colors_used = 0;
  326.     int code;
  327.  
  328.     /* We can only handle a limited set of image types. */
  329.     switch ((gs_debug_c('`') ? -1 : pic->type->index)) {
  330.     case 1:
  331.     masked = ((const gs_image1_t *)pim)->ImageMask;
  332.     has_alpha = ((const gs_image1_t *)pim)->Alpha != 0;
  333.     case 4:
  334.     if (pmat == 0)
  335.         break;
  336.     default:
  337.     goto use_default;
  338.     }
  339.     format = pim->format;
  340.     /* See above for why we allocate the enumerator as immovable. */
  341.     pie = gs_alloc_struct_immovable(mem, clist_image_enum,
  342.                     &st_clist_image_enum,
  343.                     "clist_begin_typed_image");
  344.     if (pie == 0)
  345.     return_error(gs_error_VMerror);
  346.     pie->memory = mem;
  347.     *pinfo = (gx_image_enum_common_t *) pie;
  348.     /* num_planes and plane_depths[] are set later, */
  349.     /* by gx_image_enum_common_init. */
  350.     if (masked) {
  351.     base_index = gs_color_space_index_DeviceGray;    /* arbitrary */
  352.     indexed = false;
  353.     num_components = 1;
  354.     uses_color = true;
  355.     /* cmd_put_drawing_color handles colors_used */
  356.     } else {
  357.     const gs_color_space *pcs = pim->ColorSpace;
  358.  
  359.     base_index = gs_color_space_get_index(pcs);
  360.     if (base_index == gs_color_space_index_Indexed) {
  361.         const gs_color_space *pbcs =
  362.         gs_color_space_indexed_base_space(pcs);
  363.  
  364.         indexed = true;
  365.         base_index = gs_color_space_get_index(pbcs);
  366.         num_components = 1;
  367.     } else {
  368.         indexed = false;
  369.         num_components = gs_color_space_num_components(pcs);
  370.     }
  371.     uses_color = pim->CombineWithColor && rop3_uses_T(pis->log_op);
  372.     }
  373.     code = gx_image_enum_common_init((gx_image_enum_common_t *) pie,
  374.                      (const gs_data_image_t *) pim,
  375.                      &clist_image_enum_procs, dev,
  376.                      num_components, format);
  377.     {
  378.     int i;
  379.  
  380.     for (i = 1; i < pie->num_planes; ++i)
  381.         varying_depths |= pie->plane_depths[i] != pie->plane_depths[0];
  382.     }
  383.     if (code < 0 ||
  384.     !USE_HL_IMAGES ||    /* Always use the default. */
  385.     (cdev->disable_mask & clist_disable_hl_image) || 
  386.     cdev->image_enum_id != gs_no_id ||  /* Can't handle nested images */
  387.     /****** CAN'T HANDLE CIE COLOR YET ******/
  388.     base_index > gs_color_space_index_DeviceCMYK ||
  389.     /****** CAN'T HANDLE NON-PURE COLORS YET ******/
  390.     (uses_color && !gx_dc_is_pure(pdcolor)) ||
  391.     /****** CAN'T HANDLE IMAGES WITH ALPHA YET ******/
  392.     has_alpha ||
  393.     /****** CAN'T HANDLE IMAGES WITH IRREGULAR DEPTHS ******/
  394.     varying_depths ||
  395.     (code = gs_matrix_invert(&pim->ImageMatrix, &mat)) < 0 ||
  396.     (code = gs_matrix_multiply(&mat, &ctm_only(pis), &mat)) < 0 ||
  397.     !(cdev->disable_mask & clist_disable_nonrect_hl_image ?
  398.       (is_xxyy(&mat) || is_xyyx(&mat)) :
  399.       image_matrix_ok_to_band(&mat))
  400.     )
  401.     goto use_default;
  402.     {
  403.     int bytes_per_plane, bytes_per_row;
  404.  
  405.     bits_per_pixel = pim->BitsPerComponent * num_components;
  406.     pie->image = *pim;
  407.     pie->dcolor = *pdcolor;
  408.     if (prect)
  409.         pie->rect = *prect;
  410.     else {
  411.         pie->rect.p.x = 0, pie->rect.p.y = 0;
  412.         pie->rect.q.x = pim->Width, pie->rect.q.y = pim->Height;
  413.     }
  414.     pie->pis = pis;
  415.     pie->pcpath = pcpath;
  416.     pie->format = format;
  417.     pie->bits_per_plane = bits_per_pixel / pie->num_planes;
  418.     pie->matrix = mat;
  419.     pie->uses_color = uses_color;
  420.     if (masked) {
  421.         pie->color_space.byte1 = 0;  /* arbitrary */
  422.         pie->color_space.space = 0;
  423.         pie->color_space.id = gs_no_id;
  424.     } else {
  425.         pie->color_space.byte1 = (base_index << 4) |
  426.         (indexed ? (pim->ColorSpace->params.indexed.use_proc ? 12 : 8) : 0);
  427.         pie->color_space.id =
  428.         (pie->color_space.space = pim->ColorSpace)->id;
  429.     }
  430.     pie->y = pie->rect.p.y;
  431.  
  432.     /* Image row has to fit in cmd writer's buffer */
  433.     bytes_per_plane =
  434.         (pim->Width * pie->bits_per_plane + 7) >> 3;
  435.     bytes_per_row = bytes_per_plane * pie->num_planes;
  436.     bytes_per_row = max(bytes_per_row, 1);
  437.     if (cmd_largest_size + bytes_per_row > cdev->cend - cdev->cbuf)
  438.         goto use_default;
  439.     }
  440.     if (pim->Interpolate)
  441.     pie->support.x = pie->support.y = MAX_ISCALE_SUPPORT + 1;
  442.     else
  443.     pie->support.x = pie->support.y = 0;
  444.     sbox.p.x = pie->rect.p.x - pie->support.x;
  445.     sbox.p.y = pie->rect.p.y - pie->support.y;
  446.     sbox.q.x = pie->rect.q.x + pie->support.x;
  447.     sbox.q.y = pie->rect.q.y + pie->support.y;
  448.     gs_bbox_transform(&sbox, &mat, &dbox);
  449.  
  450.     if (cdev->disable_mask & clist_disable_complex_clip)
  451.     if (!check_rect_for_trivial_clip(pcpath,
  452.                 (int)floor(dbox.p.x), (int)floor(dbox.p.y),
  453.                 (int)ceil(dbox.q.x), (int)ceil(dbox.q.y)))
  454.         goto use_default;
  455.     /* Create the begin_image command. */
  456.     if ((pie->begin_image_command_length =
  457.      begin_image_command(pie->begin_image_command,
  458.                  sizeof(pie->begin_image_command), pic)) < 0)
  459.     goto use_default;
  460.     if (!masked) {
  461.     /*
  462.      * Calculate (conservatively) the set of colors that this image
  463.      * might generate.  For single-component images with up to 4 bits
  464.      * per pixel, standard Decode values, and no Interpolate, we
  465.      * generate all the possible colors now; otherwise, we assume that
  466.      * any color might be generated.  It is possible to do better than
  467.      * this, but we won't bother unless there's evidence that it's
  468.      * worthwhile.
  469.      */
  470.     gx_color_index all =
  471.         ((gx_color_index)1 << dev->color_info.depth) - 1;
  472.  
  473.     if (bits_per_pixel > 4 || pim->Interpolate || num_components > 1)
  474.         colors_used = all;
  475.     else {
  476.         int max_value = (1 << bits_per_pixel) - 1;
  477.         float dmin = pim->Decode[0], dmax = pim->Decode[1];
  478.         float dtemp;
  479.  
  480.         if (dmax < dmin)
  481.         dtemp = dmax, dmax = dmin, dmin = dtemp;
  482.         if (dmin != 0 ||
  483.         dmax != (indexed ? max_value : 1)
  484.         ) {
  485.         colors_used = all;
  486.         } else {
  487.         /* Enumerate the possible pixel values. */
  488.         const gs_color_space *pcs = pim->ColorSpace;
  489.         cs_proc_remap_color((*remap_color)) = pcs->type->remap_color;
  490.         gs_client_color cc;
  491.         gx_drawing_color dcolor;
  492.         int i;
  493.         double denom = (indexed ? 1 : max_value);
  494.  
  495.         for (i = 0; i <= max_value; ++i) {
  496.             cc.paint.values[0] = (double)i / denom;
  497.             remap_color(&cc, pcs, &dcolor, pis, dev,
  498.                 gs_color_select_source);
  499.             colors_used |= cmd_drawing_colors_used(cdev, &dcolor);
  500.         }
  501.         }
  502.     }
  503.     }
  504.  
  505.     pie->map_rgb_to_cmyk = dev->color_info.num_components == 4 &&
  506.     base_index == gs_color_space_index_DeviceRGB;
  507.     pie->colors_used.or = colors_used;
  508.     pie->colors_used.slow_rop =
  509.     cmd_slow_rop(dev, pis->log_op, (uses_color ? pdcolor : NULL));
  510.     pie->color_map_is_known = false;
  511.     /*
  512.      * Calculate a (slightly conservative) Y bounding interval for the image
  513.      * in device space.
  514.      */
  515.     {
  516.     int y0 = (int)floor(dbox.p.y - 0.51);    /* adjust + rounding slop */
  517.     int y1 = (int)ceil(dbox.q.y + 0.51);    /* ditto */
  518.  
  519.     pie->ymin = max(y0, 0);
  520.     pie->ymax = min(y1, dev->height);
  521.     }
  522.  
  523.     /*
  524.      * Make sure the CTM, color space, and clipping region (and, for
  525.      * masked images or images with CombineWithColor, the current color)
  526.      * are known at the time of the begin_image command.
  527.      */
  528.     cmd_clear_known(cdev, clist_image_unknowns(dev, pie) | begin_image_known);
  529.  
  530.     cdev->image_enum_id = pie->id;
  531.     return 0;
  532.  
  533.     /*
  534.      * We couldn't handle the image.  Use the default algorithms, which
  535.      * break the image up into rectangles or small pixmaps.
  536.      */
  537. use_default:
  538.     gs_free_object(mem, pie, "clist_begin_typed_image");
  539.     return gx_default_begin_typed_image(dev, pis, pmat, pic, prect,
  540.                     pdcolor, pcpath, mem, pinfo);
  541. }
  542.  
  543. /* Process the next piece of an image. */
  544. private int
  545. clist_image_plane_data(gx_image_enum_common_t * info,
  546.                const gx_image_plane_t * planes, int yh,
  547.                int *rows_used)
  548. {
  549.     gx_device *dev = info->dev;
  550.     gx_device_clist_writer * const cdev =
  551.     &((gx_device_clist *)dev)->writer;
  552.     clist_image_enum *pie = (clist_image_enum *) info;
  553.     gs_rect sbox, dbox;
  554.     int y_orig = pie->y;
  555.     int yh_used = min(yh, pie->rect.q.y - y_orig);
  556.     int y0, y1;
  557.     int y, height;        /* for BEGIN/END_RECT */
  558.     int code;
  559.  
  560. #ifdef DEBUG
  561.     if (pie->id != cdev->image_enum_id) {
  562.     lprintf2("end_image id = %lu != clist image id = %lu!\n",
  563.          (ulong) pie->id, (ulong) cdev->image_enum_id);
  564.     *rows_used = 0;
  565.     return_error(gs_error_Fatal);
  566.     }
  567. #endif
  568.     /****** CAN'T HANDLE VARYING data_x VALUES YET ******/
  569.     {
  570.     int i;
  571.  
  572.     for (i = 1; i < info->num_planes; ++i)
  573.         if (planes[i].data_x != planes[0].data_x) {
  574.         *rows_used = 0;
  575.         return_error(gs_error_rangecheck);
  576.         }
  577.     }
  578.     sbox.p.x = pie->rect.p.x - pie->support.x;
  579.     sbox.p.y = (y0 = y_orig) - pie->support.y;
  580.     sbox.q.x = pie->rect.q.x + pie->support.x;
  581.     sbox.q.y = (y1 = pie->y += yh_used) + pie->support.y;
  582.     gs_bbox_transform(&sbox, &pie->matrix, &dbox);
  583.     /*
  584.      * In order to keep the band list consistent, we must write out
  585.      * the image data in precisely those bands whose begin_image
  586.      * Y range includes the respective image scan lines.  Because of
  587.      * rounding, we must expand the dbox by a little extra, and then
  588.      * use image_band_box to calculate the precise range for each band.
  589.      * This is slow, but we don't see any faster way to do it in the
  590.      * general case.
  591.      */
  592.     {
  593.     int ry0 = (int)floor(dbox.p.y) - 2;
  594.     int ry1 = (int)ceil(dbox.q.y) + 2;
  595.     int band_height = cdev->page_band_height;
  596.  
  597.     /*
  598.      * Make sure we don't go into any bands beyond the Y range
  599.      * determined at begin_image time.
  600.      */
  601.     if (ry0 < pie->ymin)
  602.         ry0 = pie->ymin;
  603.     if (ry1 > pie->ymax)
  604.         ry1 = pie->ymax;
  605.     /*
  606.      * If the image extends off the page in the Y direction,
  607.      * we may have ry0 > ry1.  Check for this here.
  608.      */
  609.     if (ry0 >= ry1)
  610.         goto done;
  611.     /* Expand the range out to band boundaries. */
  612.     y = ry0 / band_height * band_height;
  613.     height = min(ROUND_UP(ry1, band_height), dev->height) - y;
  614.     }
  615.  
  616.     FOR_RECTS {
  617.     /*
  618.      * Just transmit the subset of the data that intersects this band.
  619.      * Note that y and height always define a complete band.
  620.      */
  621.     gs_int_rect ibox;
  622.     gs_int_rect entire_box;
  623.  
  624.     if (!image_band_box(dev, pie, y, height, &ibox))
  625.         continue;
  626.     /*
  627.      * The transmitted subrectangle has to be computed at the time
  628.      * we write the begin_image command; this in turn controls how
  629.      * much of each scan line we write out.
  630.      */
  631.     {
  632.         int band_ymax = min(band_end, pie->ymax);
  633.         int band_ymin = max(band_end - band_height, pie->ymin);
  634.  
  635.         if (!image_band_box(dev, pie, band_ymin,
  636.                 band_ymax - band_ymin, &entire_box))
  637.         continue;
  638.     }
  639.  
  640.     pcls->colors_used.or |= pie->colors_used.or;
  641.     pcls->colors_used.slow_rop |= pie->colors_used.slow_rop;
  642.  
  643.     /* Write out begin_image & its preamble for this band */
  644.     if (!(pcls->known & begin_image_known)) {
  645.         gs_logical_operation_t lop = pie->pis->log_op;
  646.         byte *dp;
  647.         byte *bp = pie->begin_image_command +
  648.         pie->begin_image_command_length;
  649.         uint len;
  650.         byte image_op = cmd_opv_begin_image;
  651.  
  652.         /* Make sure the imager state is up to date. */
  653.         TRY_RECT {
  654.             code = (pie->color_map_is_known ? 0 :
  655.             cmd_put_color_mapping(cdev, pie->pis,
  656.                           pie->map_rgb_to_cmyk));
  657.         pie->color_map_is_known = true;
  658.         if (code >= 0) {
  659.             uint want_known = ctm_known | clip_path_known |
  660.             (pie->color_space.id == gs_no_id ? 0 :
  661.              color_space_known);
  662.  
  663.             code = cmd_do_write_unknown(cdev, pcls, want_known);
  664.         }
  665.         if (code >= 0)
  666.             code = cmd_do_enable_clip(cdev, pcls, pie->pcpath != NULL);
  667.         if (code >= 0)
  668.             code = cmd_update_lop(cdev, pcls, lop);
  669.         } HANDLE_RECT(code);
  670.         if (pie->uses_color) {
  671.              TRY_RECT {
  672.             code = cmd_put_drawing_color(cdev, pcls, &pie->dcolor);
  673.         } HANDLE_RECT(code);
  674.         }
  675.         if (entire_box.p.x != 0 || entire_box.p.y != 0 ||
  676.         entire_box.q.x != pie->image.Width ||
  677.         entire_box.q.y != pie->image.Height
  678.         ) {
  679.         image_op = cmd_opv_begin_image_rect;
  680.         cmd_put2w(entire_box.p.x, entire_box.p.y, bp);
  681.         cmd_put2w(pie->image.Width - entire_box.q.x,
  682.               pie->image.Height - entire_box.q.y, bp);
  683.              }
  684.         len = bp - pie->begin_image_command;
  685.         TRY_RECT {
  686.         code =
  687.             set_cmd_put_op(dp, cdev, pcls, image_op, 1 + len);
  688.         } HANDLE_RECT(code);
  689.         memcpy(dp + 1, pie->begin_image_command, len);
  690.  
  691.         /* Mark band's begin_image as known */
  692.         pcls->known |= begin_image_known;
  693.     }
  694.  
  695.     /*
  696.      * The data that we write out must use the X values set by
  697.      * begin_image, which may cover a larger interval than the ones
  698.      * actually needed for these particular scan lines if the image is
  699.      * rotated.
  700.      */
  701.     {
  702.         /*
  703.          * image_band_box ensures that b{x,y}{0,1} fall within 
  704.          * pie->rect.
  705.          */
  706.         int bx0 = entire_box.p.x, bx1 = entire_box.q.x;
  707.         int by0 = ibox.p.y, by1 = ibox.q.y;
  708.         int bpp = pie->bits_per_plane;
  709.         int num_planes = pie->num_planes;
  710.         uint offsets[gs_image_max_planes];
  711.         int i, iy, ih, xskip, xoff, nrows;
  712.         uint bytes_per_plane, bytes_per_row, rows_per_cmd;
  713.  
  714.         if (by0 < y0)
  715.         by0 = y0;
  716.         if (by1 > y1)
  717.         by1 = y1;
  718.         /*
  719.          * Make sure we're skipping an integral number of pixels, by
  720.          * truncating the initial X coordinate to the next lower
  721.          * value that is an exact multiple of a byte.
  722.          */
  723.         xoff = bx0 - pie->rect.p.x;
  724.         xskip = xoff & -(int)"\001\010\004\010\002\010\004\010"[bpp & 7];
  725.         for (i = 0; i < num_planes; ++i)
  726.         offsets[i] =
  727.             (by0 - y0) * planes[i].raster + ((xskip * bpp) >> 3);
  728.         bytes_per_plane = ((bx1 - (pie->rect.p.x + xskip)) * bpp + 7) >> 3;
  729.         bytes_per_row = bytes_per_plane * pie->num_planes;
  730.         rows_per_cmd =
  731.         (cbuf_size - cmd_largest_size) / max(bytes_per_row, 1);
  732.  
  733.         if (rows_per_cmd == 0) {
  734.         /* The reader will have to buffer a row separately. */
  735.         rows_per_cmd = 1;
  736.         }
  737.         for (iy = by0, ih = by1 - by0; ih > 0; iy += nrows, ih -= nrows) {
  738.         nrows = min(ih, rows_per_cmd);
  739.         TRY_RECT {
  740.             code = cmd_image_plane_data(cdev, pcls, planes, info,
  741.                         bytes_per_plane, offsets,
  742.                         xoff - xskip, nrows);
  743.         } HANDLE_RECT(code);
  744.         for (i = 0; i < num_planes; ++i)
  745.             offsets[i] += planes[i].raster * nrows;
  746.         }
  747.     }
  748.     } END_RECTS_ON_ERROR(
  749.     BEGIN
  750.         ++cdev->ignore_lo_mem_warnings;
  751.         NEST_RECT {
  752.         code = write_image_end_all(dev, pie);
  753.         } UNNEST_RECT;
  754.         --cdev->ignore_lo_mem_warnings;
  755.         /* Update sub-rect */
  756.         if (!pie->image.Interpolate)
  757.             pie->rect.p.y += yh_used;  /* interpolate & mem recovery currently incompat */
  758.     END,
  759.     (code < 0 ? (band_code = code) : code) >= 0,
  760.     (cmd_clear_known(cdev,
  761.              clist_image_unknowns(dev, pie) | begin_image_known),
  762.      pie->color_map_is_known = false,
  763.      cdev->image_enum_id = pie->id, true)
  764.     );
  765.  done:
  766.     *rows_used = pie->y - y_orig;
  767.     return pie->y >= pie->rect.q.y;
  768. }
  769.  
  770. /* Clean up by releasing the buffers. */
  771. private int
  772. clist_image_end_image(gx_image_enum_common_t * info, bool draw_last)
  773. {
  774.     gx_device *dev = info->dev;
  775.     gx_device_clist_writer * const cdev =
  776.     &((gx_device_clist *)dev)->writer;
  777.     clist_image_enum *pie = (clist_image_enum *) info;
  778.     int code;
  779.  
  780. #ifdef DEBUG
  781.     if (pie->id != cdev->image_enum_id) {
  782.     lprintf2("end_image id = %lu != clist image id = %lu!\n",
  783.          (ulong) pie->id, (ulong) cdev->image_enum_id);
  784.     return_error(gs_error_Fatal);
  785.     }
  786. #endif
  787.     NEST_RECT {
  788.     do {
  789.         code = write_image_end_all(dev, pie);
  790.     } while (code < 0 && cdev->error_is_retryable &&
  791.          (code = clist_VMerror_recover(cdev, code)) >= 0
  792.          );
  793.     /* if couldn't write successsfully, do a hard flush */
  794.     if (code < 0 && cdev->error_is_retryable) {
  795.         int retry_code;
  796.         ++cdev->ignore_lo_mem_warnings;
  797.         retry_code = write_image_end_all(dev, pie); /* force it out */
  798.         --cdev->ignore_lo_mem_warnings;
  799.         if (retry_code >= 0 && cdev->driver_call_nesting == 0)
  800.         code = clist_VMerror_recover_flush(cdev, code);
  801.     }
  802.     } UNNEST_RECT;
  803.     cdev->image_enum_id = gs_no_id;
  804.     gs_free_object(pie->memory, pie, "clist_image_end_image");
  805.     return code;
  806. }
  807.  
  808. /* Create a compositor device. */
  809. int
  810. clist_create_compositor(gx_device * dev,
  811.             gx_device ** pcdev, const gs_composite_t * pcte,
  812.             const gs_imager_state * pis, gs_memory_t * mem)
  813. {
  814.     /****** NYI ******/
  815.     return gx_no_create_compositor(dev, pcdev, pcte, pis, mem);
  816. }
  817.  
  818. /* ------ Utilities ------ */
  819.  
  820. /* Add a command to set data_x. */
  821. private int
  822. cmd_put_set_data_x(gx_device_clist_writer * cldev, gx_clist_state * pcls,
  823.            int data_x)
  824. {
  825.     byte *dp;
  826.     int code;
  827.  
  828.     if (data_x > 0x1f) {
  829.     int dx_msb = data_x >> 5;
  830.  
  831.     code = set_cmd_put_op(dp, cldev, pcls, cmd_opv_set_misc,
  832.                   2 + cmd_size_w(dx_msb));
  833.     if (code >= 0) {
  834.         dp[1] = cmd_set_misc_data_x + 0x20 + (data_x & 0x1f);
  835.         cmd_put_w(dx_msb, dp + 2);
  836.     }
  837.     } else {
  838.     code = set_cmd_put_op(dp, cldev, pcls, cmd_opv_set_misc, 2);
  839.     if (code >= 0)
  840.         dp[1] = cmd_set_misc_data_x + data_x;
  841.     }
  842.     return code;
  843. }
  844.  
  845. /* Add commands to represent a halftone order. */
  846. private int
  847. cmd_put_ht_order(gx_device_clist_writer * cldev, const gx_ht_order * porder,
  848.          gs_ht_separation_name cname,
  849.          int component /* -1 = default/gray/black screen */ )
  850. {
  851.     byte command[cmd_max_intsize(sizeof(long)) * 8];
  852.     byte *cp;
  853.     uint len;
  854.     byte *dp;
  855.     uint i, n;
  856.     int code;
  857.     int pi = porder->procs - ht_order_procs_table;
  858.     uint elt_size = porder->procs->bit_data_elt_size;
  859.     const uint nlevels = min((cbuf_size - 2) / sizeof(*porder->levels), 255);
  860.     const uint nbits = min((cbuf_size - 2) / elt_size, 255);
  861.  
  862.     if (pi < 0 || pi > countof(ht_order_procs_table))
  863.     return_error(gs_error_unregistered);
  864.     /* Put out the order parameters. */
  865.     cp = cmd_put_w(component + 1, command);
  866.     if (component >= 0)
  867.     cp = cmd_put_w(cname, cp);
  868.     cp = cmd_put_w(porder->width, cp);
  869.     cp = cmd_put_w(porder->height, cp);
  870.     cp = cmd_put_w(porder->raster, cp);
  871.     cp = cmd_put_w(porder->shift, cp);
  872.     cp = cmd_put_w(porder->num_levels, cp);
  873.     cp = cmd_put_w(porder->num_bits, cp);
  874.     *cp++ = (byte)pi;
  875.     len = cp - command;
  876.     code = set_cmd_put_all_op(dp, cldev, cmd_opv_set_ht_order, len + 1);
  877.     if (code < 0)
  878.     return code;
  879.     memcpy(dp + 1, command, len);
  880.  
  881.     /* Put out the transfer function, if any. */
  882.     code = cmd_put_color_map(cldev, cmd_map_ht_transfer, porder->transfer,
  883.                  NULL);
  884.     if (code < 0)
  885.     return code;
  886.  
  887.     /* Put out the levels array. */
  888.     for (i = 0; i < porder->num_levels; i += n) {
  889.     n = porder->num_levels - i;
  890.     if (n > nlevels)
  891.         n = nlevels;
  892.     code = set_cmd_put_all_op(dp, cldev, cmd_opv_set_ht_data,
  893.                   2 + n * sizeof(*porder->levels));
  894.     if (code < 0)
  895.         return code;
  896.     dp[1] = n;
  897.     memcpy(dp + 2, porder->levels + i, n * sizeof(*porder->levels));
  898.     }
  899.  
  900.     /* Put out the bits array. */
  901.     for (i = 0; i < porder->num_bits; i += n) {
  902.     n = porder->num_bits - i;
  903.     if (n > nbits)
  904.         n = nbits;
  905.     code = set_cmd_put_all_op(dp, cldev, cmd_opv_set_ht_data,
  906.                   2 + n * elt_size);
  907.     if (code < 0)
  908.         return code;
  909.     dp[1] = n;
  910.     memcpy(dp + 2, (const byte *)porder->bit_data + i * elt_size,
  911.            n * elt_size);
  912.     }
  913.  
  914.     return 0;
  915. }
  916.  
  917. /* Add commands to represent a full (device) halftone. */
  918. /* We put out the default/gray/black screen last so that the reading */
  919. /* pass can recognize the end of the halftone. */
  920. int
  921. cmd_put_halftone(gx_device_clist_writer * cldev, const gx_device_halftone * pdht,
  922.          gs_halftone_type type)
  923. {
  924.     uint num_comp = (pdht->components == 0 ? 0 : pdht->num_comp);
  925.  
  926.     {
  927.     byte *dp;
  928.     int code = set_cmd_put_all_op(dp, cldev, cmd_opv_set_misc,
  929.                       2 + cmd_size_w(num_comp));
  930.  
  931.     if (code < 0)
  932.         return code;
  933.     dp[1] = cmd_set_misc_halftone + type;
  934.     cmd_put_w(num_comp, dp + 2);
  935.     }
  936.     if (num_comp == 0)
  937.     return cmd_put_ht_order(cldev, &pdht->order,
  938.                 gs_ht_separation_Default, -1);
  939.     {
  940.     int i;
  941.  
  942.     for (i = num_comp; --i >= 0;) {
  943.         int code = cmd_put_ht_order(cldev, &pdht->components[i].corder,
  944.                     pdht->components[i].cname, i);
  945.  
  946.         if (code < 0)
  947.         return code;
  948.     }
  949.     }
  950.     return 0;
  951. }
  952.  
  953. /* Write out any necessary color mapping data. */
  954. private int
  955. cmd_put_color_mapping(gx_device_clist_writer * cldev,
  956.               const gs_imager_state * pis, bool write_rgb_to_cmyk)
  957. {
  958.     int code;
  959.     const gx_device_halftone *pdht = pis->dev_ht;
  960.  
  961.     /* Put out the halftone. */
  962.     if (pdht->id != cldev->device_halftone_id) {
  963.     code = cmd_put_halftone(cldev, pdht, pis->halftone->type);
  964.     if (code < 0)
  965.         return code;
  966.     cldev->device_halftone_id = pdht->id;
  967.     }
  968.     /* If we need to map RGB to CMYK, put out b.g. and u.c.r. */
  969.     if (write_rgb_to_cmyk) {
  970.     code = cmd_put_color_map(cldev, cmd_map_black_generation,
  971.                  pis->black_generation,
  972.                  &cldev->black_generation_id);
  973.     if (code < 0)
  974.         return code;
  975.     code = cmd_put_color_map(cldev, cmd_map_undercolor_removal,
  976.                  pis->undercolor_removal,
  977.                  &cldev->undercolor_removal_id);
  978.     if (code < 0)
  979.         return code;
  980.     }
  981.     /* Now put out the transfer functions. */
  982.     {
  983.     uint which = 0;
  984.     bool all_same = true;
  985.     int i;
  986.  
  987.     for (i = 0; i < countof(cldev->transfer_ids); ++i) {
  988.         if (pis->effective_transfer.indexed[i]->id !=
  989.         cldev->transfer_ids[i]
  990.         )
  991.         which |= 1 << i;
  992.         if (pis->effective_transfer.indexed[i]->id !=
  993.         pis->effective_transfer.indexed[0]->id
  994.         )
  995.         all_same = false;
  996.     }
  997.     /* There are 3 cases for transfer functions: nothing to write, */
  998.     /* a single function, and multiple functions. */
  999.     if (which == 0)
  1000.         return 0;
  1001.     if (which == (1 << countof(cldev->transfer_ids)) - 1 && all_same) {
  1002.         code = cmd_put_color_map(cldev, cmd_map_transfer,
  1003.                      pis->effective_transfer.indexed[0],
  1004.                      &cldev->transfer_ids[0]);
  1005.         if (code < 0)
  1006.         return code;
  1007.         for (i = 1; i < countof(cldev->transfer_ids); ++i)
  1008.         cldev->transfer_ids[i] = cldev->transfer_ids[0];
  1009.     } else
  1010.         for (i = 0; i < countof(cldev->transfer_ids); ++i) {
  1011.         code = cmd_put_color_map(cldev,
  1012.                    (cmd_map_index) (cmd_map_transfer_0 + i),
  1013.                      pis->effective_transfer.indexed[i],
  1014.                      &cldev->transfer_ids[i]);
  1015.         if (code < 0)
  1016.             return code;
  1017.         }
  1018.     }
  1019.  
  1020.     return 0;
  1021. }
  1022.  
  1023. /*
  1024.  * Compute the subrectangle of an image that intersects a band;
  1025.  * return false if it is empty.
  1026.  * It is OK for this to be too large; in fact, with the present
  1027.  * algorithm, it will be quite a bit too large if the transformation isn't
  1028.  * well-behaved ("well-behaved" meaning either xy = yx = 0 or xx = yy = 0).
  1029.  */
  1030. #define I_FLOOR(x) ((int)floor(x))
  1031. #define I_CEIL(x) ((int)ceil(x))
  1032. private void
  1033. box_merge_point(gs_int_rect * pbox, floatp x, floatp y)
  1034. {
  1035.     int t;
  1036.  
  1037.     if ((t = I_FLOOR(x)) < pbox->p.x)
  1038.     pbox->p.x = t;
  1039.     if ((t = I_CEIL(x)) > pbox->q.x)
  1040.     pbox->q.x = t;
  1041.     if ((t = I_FLOOR(y)) < pbox->p.y)
  1042.     pbox->p.y = t;
  1043.     if ((t = I_CEIL(y)) > pbox->q.y)
  1044.     pbox->q.y = t;
  1045. }
  1046. private bool
  1047. image_band_box(gx_device * dev, const clist_image_enum * pie, int y, int h,
  1048.            gs_int_rect * pbox)
  1049. {
  1050.     fixed by0 = int2fixed(y);
  1051.     fixed by1 = int2fixed(y + h);
  1052.     int
  1053.         px = pie->rect.p.x, py = pie->rect.p.y,
  1054.     qx = pie->rect.q.x, qy = pie->rect.q.y;
  1055.     gs_fixed_rect cbox;        /* device clipping box */
  1056.     gs_rect bbox;        /* cbox intersected with band */
  1057.  
  1058.     /* Intersect the device clipping box and the band. */
  1059.     (*dev_proc(dev, get_clipping_box)) (dev, &cbox);
  1060.     /* The fixed_half here is to allow for adjustment. */
  1061.     bbox.p.x = fixed2float(cbox.p.x - fixed_half);
  1062.     bbox.q.x = fixed2float(cbox.q.x + fixed_half);
  1063.     bbox.p.y = fixed2float(max(cbox.p.y, by0) - fixed_half);
  1064.     bbox.q.y = fixed2float(min(cbox.q.y, by1) + fixed_half);
  1065. #ifdef DEBUG
  1066.     if (gs_debug_c('b')) {
  1067.     dlprintf6("[b]band box for (%d,%d),(%d,%d), band (%d,%d) =>\n",
  1068.           px, py, qx, qy, y, y + h);
  1069.     dlprintf10("      (%g,%g),(%g,%g), matrix=[%g %g %g %g %g %g]\n",
  1070.            bbox.p.x, bbox.p.y, bbox.q.x, bbox.q.y,
  1071.            pie->matrix.xx, pie->matrix.xy, pie->matrix.yx,
  1072.            pie->matrix.yy, pie->matrix.tx, pie->matrix.ty);
  1073.     }
  1074. #endif
  1075.     if (is_xxyy(&pie->matrix) || is_xyyx(&pie->matrix)) {
  1076.     /*
  1077.      * The inverse transform of the band is a rectangle aligned with
  1078.      * the coordinate axes, so we can just intersect it with the
  1079.      * image subrectangle.
  1080.      */
  1081.     gs_rect ibox;        /* bbox transformed back to image space */
  1082.  
  1083.     if (gs_bbox_transform_inverse(&bbox, &pie->matrix, &ibox) < 0)
  1084.         return false;
  1085.     pbox->p.x = max(px, I_FLOOR(ibox.p.x));
  1086.     pbox->q.x = min(qx, I_CEIL(ibox.q.x));
  1087.     pbox->p.y = max(py, I_FLOOR(ibox.p.y));
  1088.     pbox->q.y = min(qy, I_CEIL(ibox.q.y));
  1089.     } else {
  1090.     /*
  1091.      * The inverse transform of the band is not aligned with the
  1092.      * axes, i.e., is a general parallelogram.  To compute an exact
  1093.      * bounding box, we need to find the intersections of this
  1094.      * parallelogram with the image subrectangle.
  1095.      *
  1096.      * There is probably a much more efficient way to do this
  1097.      * computation, but we don't know what it is.
  1098.      */
  1099.     gs_point rect[4];
  1100.     gs_point corners[5];
  1101.     int i;
  1102.  
  1103.     /* Store the corners of the image rectangle. */
  1104.     rect[0].x = rect[3].x = px;
  1105.     rect[1].x = rect[2].x = qx;
  1106.     rect[0].y = rect[1].y = py;
  1107.     rect[2].y = rect[3].y = qy;
  1108.     /*
  1109.      * Compute the corners of the clipped band in image space.  If
  1110.      * the matrix is singular or an overflow occurs, the result will
  1111.      * be nonsense: in this case, there isn't anything useful we
  1112.      * can do, so return an empty intersection.
  1113.      */
  1114.     if (gs_point_transform_inverse(bbox.p.x, bbox.p.y, &pie->matrix,
  1115.                        &corners[0]) < 0 ||
  1116.         gs_point_transform_inverse(bbox.q.x, bbox.p.y, &pie->matrix,
  1117.                        &corners[1]) < 0 ||
  1118.         gs_point_transform_inverse(bbox.q.x, bbox.q.y, &pie->matrix,
  1119.                        &corners[2]) < 0 ||
  1120.         gs_point_transform_inverse(bbox.p.x, bbox.q.y, &pie->matrix,
  1121.                        &corners[3]) < 0
  1122.         ) {
  1123.         if_debug0('b', "[b]can't inverse-transform a band corner!\n");
  1124.         return false;
  1125.     }
  1126.     corners[4] = corners[0];
  1127.     pbox->p.x = qx, pbox->p.y = qy;
  1128.     pbox->q.x = px, pbox->q.y = py;
  1129.     /*
  1130.      * We iterate over both the image rectangle and the band
  1131.      * parallelogram in a single loop for convenience, even though
  1132.      * there is no coupling between the two.
  1133.      */
  1134.     for (i = 0; i < 4; ++i) {
  1135.         gs_point pa, pt;
  1136.         double dx, dy;
  1137.  
  1138.         /* Check the image corner for being inside the band. */
  1139.         pa = rect[i];
  1140.         gs_point_transform(pa.x, pa.y, &pie->matrix, &pt);
  1141.         if (pt.x >= bbox.p.x && pt.x <= bbox.q.x &&
  1142.         pt.y >= bbox.p.y && pt.y <= bbox.q.y
  1143.         )
  1144.         box_merge_point(pbox, pa.x, pa.y);
  1145.         /* Check the band corner for being inside the image. */
  1146.         pa = corners[i];
  1147.         if (pa.x >= px && pa.x <= qx && pa.y >= py && pa.y <= qy)
  1148.         box_merge_point(pbox, pa.x, pa.y);
  1149.         /* Check for intersections of band edges with image edges. */
  1150.         dx = corners[i + 1].x - pa.x;
  1151.         dy = corners[i + 1].y - pa.y;
  1152. #define in_range(t, tc, p, q)\
  1153.   (0 <= t && t <= 1 && (t = tc) >= p && t <= q)
  1154.         if (dx != 0) {
  1155.         double t = (px - pa.x) / dx;
  1156.  
  1157.         if_debug3('b', "   (px) t=%g => (%d,%g)\n",
  1158.               t, px, pa.y + t * dy);
  1159.         if (in_range(t, pa.y + t * dy, py, qy))
  1160.             box_merge_point(pbox, (floatp) px, t);
  1161.         t = (qx - pa.x) / dx;
  1162.         if_debug3('b', "   (qx) t=%g => (%d,%g)\n",
  1163.               t, qx, pa.y + t * dy);
  1164.         if (in_range(t, pa.y + t * dy, py, qy))
  1165.             box_merge_point(pbox, (floatp) qx, t);
  1166.         }
  1167.         if (dy != 0) {
  1168.         double t = (py - pa.y) / dy;
  1169.  
  1170.         if_debug3('b', "   (py) t=%g => (%g,%d)\n",
  1171.               t, pa.x + t * dx, py);
  1172.         if (in_range(t, pa.x + t * dx, px, qx))
  1173.             box_merge_point(pbox, t, (floatp) py);
  1174.         t = (qy - pa.y) / dy;
  1175.         if_debug3('b', "   (qy) t=%g => (%g,%d)\n",
  1176.               t, pa.x + t * dx, qy);
  1177.         if (in_range(t, pa.x + t * dx, px, qx))
  1178.             box_merge_point(pbox, t, (floatp) qy);
  1179.         }
  1180. #undef in_range
  1181.     }
  1182.     }
  1183.     if_debug4('b', "    => (%d,%d),(%d,%d)\n", pbox->p.x, pbox->p.y,
  1184.           pbox->q.x, pbox->q.y);
  1185.     /*
  1186.      * If necessary, add pixels around the edges so we will have
  1187.      * enough information to do interpolation.
  1188.      */
  1189.     if ((pbox->p.x -= pie->support.x) < pie->rect.p.x)
  1190.     pbox->p.x = pie->rect.p.x;
  1191.     if ((pbox->p.y -= pie->support.y) < pie->rect.p.y)
  1192.     pbox->p.y = pie->rect.p.y;
  1193.     if ((pbox->q.x += pie->support.x) > pie->rect.q.x)
  1194.     pbox->q.x = pie->rect.q.x;
  1195.     if ((pbox->q.y += pie->support.y) > pie->rect.q.y)
  1196.     pbox->q.y = pie->rect.q.y;
  1197.     return (pbox->p.x < pbox->q.x && pbox->p.y < pbox->q.y);
  1198. }
  1199.  
  1200. /* Determine which image-related properties are unknown */
  1201. private uint    /* mask of unknown properties(see pcls->known) */
  1202. clist_image_unknowns(gx_device *dev, const clist_image_enum *pie)
  1203. {
  1204.     gx_device_clist_writer * const cdev =
  1205.     &((gx_device_clist *)dev)->writer;
  1206.     const gs_imager_state *const pis = pie->pis;
  1207.     uint unknown = 0;
  1208.  
  1209.     /*
  1210.      * Determine if the CTM, color space, and clipping region (and, for
  1211.      * masked images or images with CombineWithColor, the current color)
  1212.      * are unknown. Set the device state in anticipation of the values
  1213.      * becoming known.
  1214.      */
  1215.     if (cdev->imager_state.ctm.xx != pis->ctm.xx ||
  1216.     cdev->imager_state.ctm.xy != pis->ctm.xy ||
  1217.     cdev->imager_state.ctm.yx != pis->ctm.yx ||
  1218.     cdev->imager_state.ctm.yy != pis->ctm.yy ||
  1219.     cdev->imager_state.ctm.tx != pis->ctm.tx ||
  1220.     cdev->imager_state.ctm.ty != pis->ctm.ty
  1221.     ) {
  1222.     unknown |= ctm_known;
  1223.     cdev->imager_state.ctm = pis->ctm;
  1224.     }
  1225.     if (pie->color_space.id == gs_no_id) { /* masked image */
  1226.     cdev->color_space.space = 0; /* for GC */
  1227.     } else {            /* not masked */
  1228.     if (cdev->color_space.id == pie->color_space.id) {
  1229.         /* The color space pointer might not be valid: update it. */
  1230.         cdev->color_space.space = pie->color_space.space;
  1231.     } else {
  1232.         unknown |= color_space_known;
  1233.         cdev->color_space = pie->color_space;
  1234.     }
  1235.     }
  1236.     if (cmd_check_clip_path(cdev, pie->pcpath))
  1237.     unknown |= clip_path_known;
  1238.  
  1239.     return unknown;
  1240. }
  1241.  
  1242. /* Construct the begin_image command. */
  1243. private int
  1244. begin_image_command(byte *buf, uint buf_size, const gs_image_common_t *pic)
  1245. {
  1246.     int i;
  1247.     stream s;
  1248.     const gs_color_space *ignore_pcs;
  1249.     int code;
  1250.  
  1251.     for (i = 0; i < gx_image_type_table_count; ++i)
  1252.     if (gx_image_type_table[i] == pic->type)
  1253.         break;
  1254.     if (i >= gx_image_type_table_count)
  1255.     return_error(gs_error_rangecheck);
  1256.     swrite_string(&s, buf, buf_size);
  1257.     sputc(&s, (byte)i);
  1258.     code = pic->type->sput(pic, &s, &ignore_pcs);
  1259.     return (code < 0 ? code : stell(&s));
  1260. }
  1261.  
  1262. /* Write data for a partial image. */
  1263. private int
  1264. cmd_image_plane_data(gx_device_clist_writer * cldev, gx_clist_state * pcls,
  1265.              const gx_image_plane_t * planes,
  1266.              const gx_image_enum_common_t * pie,
  1267.              uint bytes_per_plane, const uint * offsets,
  1268.              int dx, int h)
  1269. {
  1270.     int data_x = planes[0].data_x + dx;
  1271.     uint nbytes = bytes_per_plane * pie->num_planes * h;
  1272.     uint len = 1 + cmd_size2w(h, bytes_per_plane) + nbytes;
  1273.     byte *dp;
  1274.     uint offset = 0;
  1275.     int plane, i;
  1276.     int code;
  1277.  
  1278.     if (data_x) {
  1279.     code = cmd_put_set_data_x(cldev, pcls, data_x);
  1280.     if (code < 0)
  1281.         return code;
  1282.     offset = ((data_x & ~7) * cldev->color_info.depth) >> 3;
  1283.     }
  1284.     code = set_cmd_put_op(dp, cldev, pcls, cmd_opv_image_data, len);
  1285.     if (code < 0)
  1286.     return code;
  1287.     dp++;
  1288.     cmd_put2w(h, bytes_per_plane, dp);
  1289.     for (plane = 0; plane < pie->num_planes; ++plane)
  1290.     for (i = 0; i < h; ++i) {
  1291.         memcpy(dp,
  1292.            planes[plane].data + i * planes[plane].raster +
  1293.            offsets[plane] + offset,
  1294.            bytes_per_plane);
  1295.         dp += bytes_per_plane;
  1296.     }
  1297.     return 0;
  1298. }
  1299.  
  1300. /* Write image_end commands into all bands */
  1301. private int    /* ret 0 ok, else -ve error status */
  1302. write_image_end_all(gx_device *dev, const clist_image_enum *pie)
  1303. {
  1304.     gx_device_clist_writer * const cdev =
  1305.     &((gx_device_clist *)dev)->writer;
  1306.     int code;
  1307.     int y = pie->ymin;
  1308.     int height = pie->ymax - y;
  1309.  
  1310.     /*
  1311.      * We need to check specially for images lying entirely outside the
  1312.      * page, since FOR_RECTS doesn't do this.
  1313.      */
  1314.     if (height <= 0)
  1315.     return 0;
  1316.     FOR_RECTS {
  1317.     byte *dp;
  1318.  
  1319.     if (!(pcls->known & begin_image_known))
  1320.         continue;
  1321.     TRY_RECT {
  1322.         if_debug1('L', "[L]image_end for band %d\n", band);
  1323.         code = set_cmd_put_op(dp, cdev, pcls, cmd_opv_image_data, 2);
  1324.     } HANDLE_RECT(code);
  1325.     dp[1] = 0;        /* EOD */
  1326.     pcls->known ^= begin_image_known;
  1327.     } END_RECTS;
  1328.     return 0;
  1329. }
  1330.  
  1331. /*
  1332.  * Compare a rectangle vs. clip path.  Return true if there is no clipping
  1333.  * path, if the rectangle is unclipped, or if the clipping path is a
  1334.  * rectangle and intersects the given rectangle.
  1335.  */
  1336. private bool
  1337. check_rect_for_trivial_clip(
  1338.     const gx_clip_path *pcpath,    /* May be NULL, clip to evaluate */
  1339.     int px, int py, int qx, int qy    /* corners of box to test */
  1340. )
  1341. {
  1342.     gs_fixed_rect obox;
  1343.     gs_fixed_rect imgbox;
  1344.  
  1345.     if (!pcpath)
  1346.     return true;
  1347.  
  1348.     imgbox.p.x = int2fixed(px);
  1349.     imgbox.p.y = int2fixed(py);
  1350.     imgbox.q.x = int2fixed(qx);
  1351.     imgbox.q.y = int2fixed(qy);
  1352.     if (gx_cpath_includes_rectangle(pcpath,
  1353.                     imgbox.p.x, imgbox.p.y,
  1354.                     imgbox.q.x, imgbox.q.y))
  1355.     return true;
  1356.  
  1357.     return (gx_cpath_outer_box(pcpath, &obox) /* cpath is rectangle */ &&
  1358.         obox.p.x <= imgbox.q.x && obox.q.x >= imgbox.p.x &&
  1359.         obox.p.y <= imgbox.q.y && obox.q.y >= imgbox.p.y );
  1360. }
  1361.